<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 11/26/17 00:00 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class ArticleController extends BaseController
{
    //  获取所有分类
    public function classifyAllAction()
    {
        $conn = $this->get('database_connection');

        $data = $conn->fetchAll("SELECT * FROM article_classify");

        return $this->jsonResponse('操作成功', 0, $data);
    }

    // article列表
    public function articleListAction(Request $request)
    {
        $token = $request->get('token');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            $user_id = $user['id'];
            $list = $conn->fetchAll("SELECT * FROM article WHERE user_id = '". $user_id ."' ORDER BY id DESC");
            return $this->jsonResponse('操作成功', 0, array('list' => $list, 'count' => count($list)));
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }

    /**
     * article操作 （新增或修改）
     *
     * $id 为null时 => 新增，反之为编辑
     *
     */
    public function articleOperaAction(Request $request)
    {
        $token = $request->get('token');
        $id = $request->get('id');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            $user_id = $user['id'];
            $article_classify_name = $user['pet_classify_name'];
            $title = $request->get('title');
            $introduction = $request->get('introduction');
            $content = $request->get('content');

            $data = array(
              'user_id' => $user_id,
              'title' => $title,
              'article_classify_name' => $article_classify_name,
              'introduction' => $introduction,
              'content' => $content
            );

            if ($id === null) {
                $data['created_at'] = date('y-m-d h:i:s',time());
                $conn->insert('article' , $data);
            } else {
                $data['updated_at'] = date('y-m-d h:i:s',time());
                $conn->update('article', $data, array('id'=> $id));
            }
            return $this->jsonResponse('操作成功', 0);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }

    /**
     * 删除文章
     *
     */
    public function articleDelAction(Request $request)
    {
        $token = $request->get('token');
        $id = $request->get('id');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            $user_id = $user['id'];
            $where = array(
                'id' => $id,
                'user_id' => $user_id
            );

            $conn->delete('article', $where);
            return $this->jsonResponse('操作成功', 0);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }

    /**
     * 获取文章详情
     *
     */
    public function articleInfoAction(Request $request)
    {
        $token = $request->get('token');
        $id = $request->get('id');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            $info = $conn->fetchAssoc("SELECT * FROM article WHERE id = '". $id ."'");
            return $this->jsonResponse('操作成功', 0, $info);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }
}